import * as toml from 'toml';

export class FrontMatter {
    private parsedData: any;
    KEYS = ['title', 'date', 'draft', 'author', 'tags', 'categories', 'summary', 'cover'];
    private constructor() {
    }
    static from(content:string) : FrontMatter|null {
        if (content === '') {
            return null;
        }
        try {
            const parsedData = toml.parse(content);
            const frontMatter = new FrontMatter();
            frontMatter.parsedData = parsedData;
            return frontMatter;
          } catch (e) {
            console.error("Error parsing TOML:", e);
            return null;
          }
    }
    public getTitle(): string {
        return this.parsedData['title'] || 'unknown';
    }
    public getDate(): string {
        return this.parsedData['date'] || 'unknown';
    }
    public getDraft(): boolean {
        const draft = this.parsedData['draft'];
        if (draft === undefined || draft === null) {
            return false;
        }

        // Handle boolean values
        if (typeof draft === 'boolean') {
            return draft;
        }

        // Handle string values with different case and quote styles
        if (typeof draft === 'string') {
            // Convert to lowercase and remove whitespace
            const normalizedDraft = draft.toLowerCase().trim();

            // Remove quotes if present
            const unquotedDraft = normalizedDraft
                .replace(/^['"](.*)['"]$/, '$1') // Remove surrounding quotes
                .trim();

            return unquotedDraft === 'true';
        }

        return false;
    }
    public getAuthor(): string {
        return this.parsedData['author'] || 'unknown';
    }
    public getTags(): string[] {
        return this.parsedData['tags'] || [];
    }
    public getCategories(): string[] {
        return this.parsedData['categories'] || [];
    }
    public getSummary(): string {
        return this.parsedData['description'] || 'unknown';
    }
    public getCover(): string {
        return this.parsedData['cover'] || 'unknown';
    }
    public to_string(): string {
        let result = '';

        // Add title if exists
        if (this.parsedData['title']) {
            result += `title = "${this.parsedData['title']}"\n`;
        }

        // Add draft status if exists
        if (this.parsedData['draft'] !== undefined) {
            // Convert to string and wrap in double quotes
            const draftValue = String(this.parsedData['draft']).toLowerCase();
            result += `draft = "${draftValue}"\n`;
        }

        // Add author if exists
        if (this.parsedData['author'] && this.parsedData['author'] !== 'unknown') {
            result += `author = "${this.parsedData['author']}"\n`;
        }

        // Add date if exists
        if (this.parsedData['date'] && this.parsedData['date'] !== 'unknown') {
            result += `date = "${this.parsedData['date']}"\n`;
        }

        // Add tags if exists
        if (this.parsedData['tags'] && this.parsedData['tags'].length > 0) {
            result += `tags = [${this.parsedData['tags'].map((tag: string) => `"${tag}"`).join(', ')}]\n`;
        }

        // Add categories if exists
        if (this.parsedData['categories'] && this.parsedData['categories'].length > 0) {
            result += `categories = [${this.parsedData['categories'].map((category: string) => `"${category}"`).join(', ')}]\n`;
        }

        // Add summary if exists
        if (this.parsedData['description'] && this.parsedData['description'] !== 'unknown') {
            result += `description = "${this.parsedData['description']}"\n`;
        }

        // Add cover if exists
        if (this.parsedData['cover'] && this.parsedData['cover'] !== 'unknown') {
            result += `cover = "${this.parsedData['cover']}"\n`;
        }

        return result;
    }
}